import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
import java.util.*;

public class CharServer {
    boolean started = false;
    ServerSocket ss = null;

    List<Client> clients = new ArrayList<Client>();//存放Client客户端

    public static void main(String[] args) {
        new CharServer().start();
    }

    public void start(){//面向对象细节隐藏和包装需要使用start
        try {
            ss = new ServerSocket(8888);
            started = true;
        } catch (BindException e) {
            System.out.println("端口使用中....");
            System.out.println("请关掉相关程序并重新运行服务器!");
            System.exit(0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try { 
            while(started){
                Socket s = ss.accept();
                Client c = new Client(s);//需要在start里边new内部类，因为内部类无法直接在main方法中进行new
System.out.println("a client connected!");
                new Thread(c).start();
                clients.add(c);//将客户端数组加进来
                //dis.close();
            }
        } catch (IOException e) {  
            // TODO Auto-generated catch block
            e.printStackTrace();          
        } finally {
            try {
                ss.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    class Client implements Runnable{
        private Socket s;
        private DataInputStream dis = null;//s输入流管道
        private DataOutputStream dos = null;//输出流管道
        private boolean bConnected = false;//判断链接
        public Client(Socket s){
            this.s = s;
            try {
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                bConnected = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void send(String str){
            try {
                dos.writeUTF(str);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void run(){
            try{
                while(bConnected){
                    String str = dis.readUTF();//读取数据
System.out.println(str);
                    //将接收到的字符串转发给每个客户端
                    for(int i = 0; i<clients.size(); i++){
                        Client c = clients.get(i);
                        c.send(str);
                    }
                    /*
                    for(Iterator<Client> it = clients.iterator(); it.hasNext();){
                        Client c = it.next();
                        c.send(str);
                    }
                    */
                    /*
                    Iterator<Client> it = clients.iterator();
                    while(it.hasNext()){
                        Client c = it.next();
                        c.send(str);
                    }
                    */
                }
            } catch (EOFException e) {
                System.out.println("Client closed!");
            } catch (IOException e) {  
                // TODO Auto-generated catch block
                e.printStackTrace();
                
            } finally {
                try {
                    if(dis != null)   dis.close();
                    if(dos != null)   dos.close();
                    if(s != null)  s.close(); // 发现Exception作出处理
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            
        }
    }
}